package com.yuehuanghun.admin.core.controller.system;

import java.io.IOException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yuehuanghun.admin.core.config.SystemProperties;
import com.yuehuanghun.admin.core.config.UserConstants;
import com.yuehuanghun.admin.core.config.util.FileUploadUtils;
import com.yuehuanghun.admin.core.config.util.SecurityUtils;
import com.yuehuanghun.admin.core.config.util.UserUtils;
import com.yuehuanghun.admin.core.domain.SysUser;
import com.yuehuanghun.admin.core.service.ISysUserService;
import com.yuehuanghun.framework.annotation.Log;
import com.yuehuanghun.framework.annotation.Log.BusinessType;
import com.yuehuanghun.framework.exception.ValidateException;
import com.yuehuanghun.framework.util.Kv;
import com.yuehuanghun.framework.util.StringUtils;
import com.yuehuanghun.framework.web.BaseResource;

import cn.dev33.satoken.annotation.SaCheckLogin;

/**
 * 个人信息 业务处理
 * 
 * @author yadmin
 */
@RestController
@RequestMapping("/system/user/profile")
@SaCheckLogin
public class SysProfileController extends BaseResource {
	@Autowired
	private ISysUserService userService;

	/**
	 * 个人信息
	 */
	@SuppressWarnings("rawtypes")
	@GetMapping
	public Map profile() {
		SysUser user = userService.getUserByUserName(UserUtils.getCurrentUserName());
		return Kv.by("user", user).set("roleGroup", userService.getUserRoleGroup(user.getUserName()))
				.set("postGroup", userService.getUserPostGroup(UserUtils.getCurrentUserName()));
	}

	/**
	 * 修改用户
	 */
	@Log(title = "个人信息", businessType = BusinessType.UPDATE)
	@PutMapping
	public void updateProfile(@RequestBody SysUser user) { //NOSONAR
		if (StringUtils.isNotEmpty(user.getPhonenumber())
				&& UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
			throw new ValidateException("修改用户'" + user.getUserName() + "'失败，手机号码已存在");
		}
		if (StringUtils.isNotEmpty(user.getEmail())
				&& UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
			throw new ValidateException("修改用户'" + user.getUserName() + "'失败，邮箱账号已存在");
		}

		throw new ValidateException("修改个人信息异常，请联系管理员");
	}

	/**
	 * 重置密码
	 */
	@Log(title = "个人信息", businessType = BusinessType.UPDATE)
	@PutMapping("/updatePwd")
	public void updatePwd(String oldPassword, String newPassword) {
		SysUser user = userService.getUserByUserName(UserUtils.getCurrentUserName());
		String password = user.getPassword();
		if (!SecurityUtils.matchesPassword(oldPassword, password)) {
			throw new ValidateException("修改密码失败，旧密码错误");
		}
		if (SecurityUtils.matchesPassword(newPassword, password)) {
			throw new ValidateException("新密码不能与旧密码相同");
		}
		int effect = userService.resetUserPwd(UserUtils.getCurrentUserName(), SecurityUtils.encryptPassword(newPassword));
		if(effect == 0) {
			throw new ValidateException("修改密码异常，请联系管理员");
		}
	}

	/**
	 * 头像上传
	 */
	@SuppressWarnings("rawtypes")
	@Log(title = "用户头像", businessType = BusinessType.UPDATE)
	@PostMapping("/avatar")
	public Map avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException {
		if (!file.isEmpty()) {
			String avatar = FileUploadUtils.upload(SystemProperties.getAvatarPath(), file);
			if (userService.updateUserAvatar(UserUtils.getCurrentUserName(), avatar)) {
				return Kv.by("imgUrl", avatar);
			}
		}
		throw new ValidateException("上传图片异常，请联系管理员");
	}
}
